﻿using Controller;

using hCommand;
using Net.http;
using System;
using System.Collections.Generic;

namespace Persistence {
    public class WcsFinshParamCase : WcsFinshParamLocBSEngine<CoreStock> {
        public override void inFinsh() {
            String sqlbatch = "select * from core_stock_det where pid=@id and status in ({0},{1})";
            List<CoreStockDet> csds = query<CoreStockDet>(HUtil.s(sqlbatch, StockDetailStatus.库外, StockDetailStatus.入库中), new { cs.id });

            string sql1 = "update core_stock_det set  status={0},change_time=sgj_get_sys_now,doo_det_id=0  where status in ({1},{2})  and pid = @stockId";
            exec(HUtil.s(sql1, StockDetailStatus.库存, StockDetailStatus.库外, StockDetailStatus.入库中), new { stockId = cs.id });

            string sql2 = "update core_stock_det set   p_loc=@stn,doo_det_id=0 where status <70  and pid = @stockId  ";
            exec(sql2, new { stockId = cs.id, stn = ctp.des });

            StatUtil.cvCSDIn(this, csds);
        }


        protected override bool needFinsh() {
            if (!base.needFinsh()) {
                return false;
            }
            
            return true || ctp.des == ConstStn.stn1
                || ctp.des == ConstStn.stn3 || ctp.des == ConstStn.Crane1Out1Stn
                || ctp.des == ConstStn.Crane1Out2Stn || ctp.des == ConstStn.stn6 || ctp.des == ConstStn.stn8;
        }
        protected override StockStatus getOutTaskedStatus() {
            return StockStatus.库外;
        }
       bool  derect=false;
        public override void outFinsh() {
            int des = ctp.des;
            string sql = "update core_stock_det  set change_time=sgj_get_sys_now , status={0} ,out_Time=sgj_get_sys_now    where status={1} and pid=@stockId ";
            if (derect) {
                String sqlQuery = "select * from core_stock_det   where status={0} and pid=@id  ";
                List<CoreStockDet> list = query<CoreStockDet>(HUtil.s(sqlQuery, StockDetailStatus.出库中),cs);
                StatUtil.cvCSDOut(this, list);
                sql = HUtil.s(sql, StockDetailStatus.单据自动出库, StockDetailStatus.出库中);
            } else {
                sql = HUtil.s(sql, StockDetailStatus.库外, StockDetailStatus.出库中);
            }
            exec(sql, new { stockId = cs.id, des });

            string sql2 = "update core_stock_det set   p_loc=@stn  where status <{0}  and pid = @stockId  ";
            sql2 = HUtil.s(sql2, StockDetailStatus .上限);
            exec(sql2, new { stockId = cs.id, stn = des });

            String sql1 = "update core_stock_det set change_time=sgj_get_sys_now    where pid=@stockId and status=" + (int)StockDetailStatus.库存;
            exec(sql1, new { stockId = cs.id, des });
        }




    }
}
